package cn.bonoon.services;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.rawdata.RawdataDefinition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.RawdataImportService;
import cn.bonoon.entities.RawdataImportEntity;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.support.services.AbstractSearchService;
import cn.bonoon.kernel.util.StringHelper;

@Service
@Transactional(readOnly = true)
public class RawdataImportServiceImpl extends AbstractSearchService<RawdataImportEntity> implements RawdataImportService{

	@Override
	public List<Object> getItems(IOperator user, RawdataImportEntity entity, RawdataDefinition def,boolean filterDel) {
		
		return def.getParser().importItems(entityManager, entity,filterDel);
	}

	@Override
	public List<Object> getItemsByStartEnd(IOperator user,
			RawdataImportEntity entity, RawdataDefinition def, int start,
			int end) {
		
		return getItems(user,entity,def,true).subList(start, end);
	}

	@Override
	@Transactional
	public void deleteByImportItem(RawdataImportEntity rawdataImport,IOperator user, RawdataDefinition def) throws Exception {
		
		def.getParser().deleteByImportItem(entityManager, rawdataImport, user);
	}

	@Override
	@Transactional
	public void update(IOperator user, RawdataImportEntity entity)
			throws Exception {
		entityManager.merge(entity);
	}

	@Override
	@Transactional
	public void changeforbid(IOperator user,RawdataImportEntity rawdataImport,RawdataDefinition def) {
		if (rawdataImport.isForbid()) {
			rawdataImport.setForbid(false);
		} else {
			rawdataImport.setForbid(true);
			def.getParser().setNullByRawdataImport(entityManager, rawdataImport);
			
		}
	}

	@Override
	@Transactional
	public void recheck(RawdataImportEntity rawdataImport, IOperator user,
			RawdataDefinition def) throws Exception {
		def.getParser().recheck(entityManager,rawdataImport,user);
		
	}

	@Override
	public List<Object> getItemsByType(List<Object> list, String type) {
		List<Object> list2=new ArrayList<>();
		for (Object object : list) {
			//全部
			if ("all".equals(type)) {
				list2=list;
				break;
			}
			//村级导入成功
			else if ("success".equals(type)&&"TREATED".equals(StringHelper.getValueByFiled(object, "treatment").toString())) {
				list2.add(object);
			}
			//匹配成功
			else if ("success2".equals(type)&&("UNTREATED".equals(StringHelper.getValueByFiled(object, "status").toString())&&"NONE".equals(StringHelper.getValueByFiled(object, "treatment").toString()))) {
				list2.add(object);
			}
			//无法匹配
			else if ("fail".equals(type)&&"ABNORMAL".equals(StringHelper.getValueByFiled(object, "status").toString())) {
				list2.add(object);
			}
			//存疑
			else if ("doubt".equals(type)&&"DOUBTFUL".equals(StringHelper.getValueByFiled(object, "treatment").toString())) {
				list2.add(object);
			}
			//退回
			else if ("back".equals(type)&&"REJECT".equals(StringHelper.getValueByFiled(object, "treatment").toString())) {
				list2.add(object);
			}
			//重复
			else if ("repeat".equals(type)&&"REPEAT".equals(StringHelper.getValueByFiled(object, "treatment").toString())) {
				list2.add(object);
			}
		}
		return list2;
	}
}
